package AuthorityAgent;

import jade.core.Agent;
import jade.core.behaviours.WakerBehaviour;
import wwtp.ontology.Biological;
import wwtp.ontology.Chemical;
import wwtp.ontology.Physical;
import wwtp.ontology.Volume;
import wwtp.ontology.Water;

/*
 * ESTE BEHAVIOUR YA NO SE USA 
 */
public class EraseWaterBehaviour extends WakerBehaviour{

	private Water water;

	public EraseWaterBehaviour(Agent arg0, Water water, long arg1) {
		super(arg0, arg1);
		this.water = water;
	}

	@Override
	protected void handleElapsedTimeout() {
		eraseWater();

	}

	private void eraseWater(){
		Water agentW =((AuthorityAgent)myAgent).water;
		Biological biological = agentW.getBiological();
		Chemical chemical = agentW.getChemical();
		Physical physical = agentW.getPhysical();
		Volume vol = new Volume();

		double incomingBiological = water.getBiological().getPercentaje();
		double incomingChemical = water.getChemical().getPercentaje();
		double incomingPhysical = water.getPhysical().getPercentaje();
		double incomingVolumen = water.getVolume().getLitres();

		double incomingBiologicalMass = incomingVolumen * incomingBiological;
		double incomingChemicalMass = incomingVolumen * incomingChemical;
		double incomingPhysicalMass = incomingVolumen * incomingPhysical;

		double oldBiological = agentW.getBiological().getPercentaje();
		double oldChemical = agentW.getChemical().getPercentaje();
		double oldPhysical = agentW.getPhysical().getPercentaje();
		double oldVolume = agentW.getVolume().getLitres();

		double oldBiologicalMass = oldBiological * oldVolume;
		double oldChemicalMass = oldChemical * oldVolume;
		double oldPhysicalMass = oldPhysical * oldVolume;

		double newVolume = oldVolume - incomingVolumen;

		biological.setPercentaje((float) ((oldBiologicalMass - incomingBiologicalMass)/newVolume));
		chemical.setPercentaje((float) ((oldChemicalMass - incomingChemicalMass)/newVolume));
		physical.setPercentaje((float) ((oldPhysicalMass - incomingPhysicalMass)/newVolume));
		vol.setLitres((float) newVolume);

		agentW.setBiological(biological);
		agentW.setChemical(chemical);
		agentW.setPhysical(physical);
		agentW.setVolume(vol);
		((AuthorityAgent)myAgent).water = agentW;
	}
}
